﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using InternshipManagement.Models;
using InternshipManagement.Helpers;
using System.Text;

namespace InternshipManagement.Controllers
{
    public class FormController : Controller
    {
        FormRepository formRepository = new FormRepository();
        AccountRepository accountRepository = new AccountRepository();
        SemesterRepository semesterRepository = new SemesterRepository();
        //
        // GET: /ApplicationForm/
        [ActiveSemester]
        public ActionResult ApplicationForm()
        {
            Student student = accountRepository.GetStudent((int)Session["UserID"]);
            ApplicationForm form = formRepository.GetApplicationForm(student.ID, student.CourseID.Value);
            if (form == null)
                form = new ApplicationForm();

            form.Ankara = false;
            form.Istanbul = false;
            form.Izmir = false;
            form.OtherCityCheck = false;
            form.OtherCities = "";
            form.AbroadCityCheck = false;
            form.AbroadCities = "";
            if (form.Cities != null)
            {
                string[] cities = form.Cities.Split(new string[] { ":::" }, StringSplitOptions.None);
                foreach (string city in cities)
                {
                    if (city == "Ankara")
                    {
                        form.Ankara = true;
                    }
                    else if (city == "Istanbul")
                    {
                        form.Istanbul = true;
                    }
                    else if (city == "Izmir")
                    {
                        form.Izmir = true;
                    }
                    else if (city.StartsWith("OtherCities"))
                    {
                        form.OtherCityCheck = true;
                        form.OtherCities = city.Substring(12);
                    }
                    else
                    {
                        form.AbroadCityCheck = true;
                        form.AbroadCities = city.Substring(12);
                    }
                }
            }

            form.YazilimGelistirme = false;
            form.AgYonetimi = false;
            form.SistemYonetimi = false;
            form.OtherBranchCheck = false;
            form.OtherBranches = "";
            if (form.Branch != null)
            {
                string[] branches = form.Branch.Split(new string[] { ":::" }, StringSplitOptions.None);
                foreach (string branch in branches)
                {
                    if (branch == "YazilimGelistirme")
                    {
                        form.YazilimGelistirme = true;
                    }
                    else if (branch == "AgYonetimi")
                    {
                        form.AgYonetimi = true;
                    }
                    else if (branch == "SistemYonetimi")
                    {
                        form.SistemYonetimi = true;
                    }
                    else
                    {
                        form.OtherBranchCheck = true;
                        form.OtherBranches = branch.Substring(14);
                    }
                }
            }

            return View(form);
        }
        [ActiveSemester]
        [HttpPost]
        public ActionResult ApplicationForm(ApplicationForm form, FormCollection formvalues)
        {

            if (ModelState.IsValid)
            {

                List<string> cities = new List<string>();
                if (formvalues["Ankara"] == "true")
                {
                    cities.Add("Ankara");
                }
                if (formvalues["Istanbul"] == "true")
                {
                    cities.Add("Istanbul");
                }
                if (formvalues["Izmir"] == "true")
                {
                    cities.Add("Izmir");
                }
                if (formvalues["OtherCityCheck"] == "true")
                {
                    cities.Add("OtherCities=" + formvalues["OtherCities"]);
                }
                if (formvalues["AbroadCityCheck"] == "true")
                {
                    cities.Add("AbroadCities=" + formvalues["AbroadCities"]);
                }

                List<string> branches = new List<string>();
                if (formvalues["YazilimGelistirme"] == "true")
                {
                    branches.Add("YazilimGelistirme");
                }
                if (formvalues["AgYonetimi"] == "true")
                {
                    branches.Add("AgYonetimi");
                }
                if (formvalues["SistemYonetimi"] == "true")
                {
                    branches.Add("SistemYonetimi");
                }
                if (formvalues["OtherBranchCheck"] == "true")
                {
                    branches.Add("OtherBranches=" + formvalues["OtherBranches"]);
                }

                if (cities.Count == 0)
                {
                    ModelState.AddModelError("Cities", "En az bir tane şehir seçilmelidir.");
                }
                if (branches.Count == 0)
                {
                    ModelState.AddModelError("Branch", "En az bir tane alan seçilmelidir.");
                }

                if (cities.Count != 0 && branches.Count != 0)
                {
                    Student student = accountRepository.GetStudent((int)Session["UserID"]);
                    if (!formRepository.ApplicationFormExists(student.ID, student.CourseID.Value))
                    {
                        form.Cities = string.Join(":::", cities.ToArray());
                        form.Branch = string.Join(":::", branches.ToArray());
                        form.UpdateDate = DateTime.Now;
                        form.UserID = (int)Session["UserID"];
                        form.CourseID = student.CourseID;
                        formRepository.AddApplicationForm(form);
                        formRepository.Save();

                        ViewBag.Message = "Application Form has been saved.";
                        return View("ShowMessage");
                    }
                    else
                    {
                        form = formRepository.GetApplicationForm(student.ID, student.CourseID.Value);
                        if (TryUpdateModel(form, formvalues))
                        {
                            form.Cities = string.Join(":::", cities.ToArray());
                            form.Branch = string.Join(":::", branches.ToArray());
                            form.UpdateDate = DateTime.Now;
                            formRepository.Save();
                            ViewBag.Message = "Application Form has been saved.";
                            return View("ShowMessage");
                        }
                    }
                }
            }

            return View(form);
        }

    }
}
